package com.afsadmedia.pregnancy;

import java.util.ArrayList;

import com.afsadmedia.pregnancy.SympotomsListview.ImageAdapter;
import com.afsadmedia.pregnancy.SympotomsListview.ViewHolder;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnCreateContextMenuListener;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnGenericMotionListener;
import android.view.View.OnHoverListener;
import android.view.View.OnKeyListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;

public class NoteList  extends Activity{
	
	
	EditText et_dataenter;
	ListView listnote;
	Button add;
	 ArrayAdapter<String> dataAdapter = null;
	 
	// private ListView listview;  
  //   ArrayList<String> items = new ArrayList<String>();  
     private int count;  
     private boolean[] thumbnailsselection;  
     
     ImageAdapter adapter;
	 
	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.notelist);
		
	//	Constants.notelist = new ArrayList<String>();
		
	//	 fillarray();  
       
         
      //  listview = (ListView) findViewById(R.id.listView_symptomsselect);  
		
		
	//	Constants.notelist.add(et_dataenter.getText().toString());
		
	 
		 
         
		
		et_dataenter =(EditText)findViewById(R.id.et_dataenter);
		listnote = (ListView)findViewById(R.id.listView_notelist);
		
		Constants.notelist.clear();
		
		add =(Button)findViewById(R.id.button_add);
		
		add.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				if(et_dataenter.getText().toString().equals("")){
					 Toast.makeText(NoteList.this, "Please Add items",  
                             Toast.LENGTH_SHORT).show(); 
				}else {
			    
				Constants.notelist.add(et_dataenter.getText().toString());
				
				Constants.addnotearry.add(et_dataenter.getText().toString());
				count = Constants.notelist.size();  
				thumbnailsselection = new boolean[count];  
				
				adapter = new ImageAdapter(NoteList.this);
						
				listnote.setAdapter(adapter);  
				et_dataenter.setText("");
				}
			}
		});
		
		
		et_dataenter.setOnKeyListener(new OnKeyListener() {
			
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				// TODO Auto-generated method stub
				
				if(keyCode == 0){
					Toast.makeText(NoteList.this, "hhhhhhhh",  
                            Toast.LENGTH_SHORT).show(); 
					
				}
				return false;
			}
		});
				
				
				
				
		
		
		
		listnote.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View v, int position,
					long arg3) {
				// TODO Auto-generated method stub
				
				
				final int positionToRemove = position;
				
				Log.v("ASI", String.valueOf(positionToRemove));
               
				int selectedCatid = 0;
				try {
					String s=listnote.getItemAtPosition(position).toString();
					Log.i("String s",s);
				v=	listnote.getChildAt(position-listnote.getFirstVisiblePosition());
				ImageView b=(ImageView)v.findViewById(R.id.imageView_cancel);
				b.setVisibility(View.VISIBLE);
				b.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						
						Log.v("ASI1", String.valueOf(positionToRemove));
						Constants.notelist.remove(positionToRemove);
						
						count = Constants.notelist.size();  
						thumbnailsselection = new boolean[count];
						adapter.notifyDataSetChanged();

						listnote.setAdapter(adapter);
						listnote.invalidateViews();

//						Intent i = new Intent(NoteList.this, NoteList.class);
//						startActivity(i);
						
					//	String abc = qt.get(oo);
						Log.v("hhhhhhaaa","abd");
					}
				});
					
				
				Log.i("selectedCatid",String.valueOf(selectedCatid));
				} catch (NumberFormatException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				
			}
		});
		
		
		
		}

	  public class ImageAdapter extends BaseAdapter {  
          private Context mContext;  
          public ImageAdapter(Context context) {  
               mContext = context;  
          }  
          public int getCount() {  
               return count;  
          }  
          public Object getItem(int position) {  
               return position;  
          }  
          public long getItemId(int position) {  
               return position;  
          }  
          public View getView(int position, View convertView, ViewGroup parent) {  
               final ViewHolder holder;  
               if (convertView == null) {  
                    holder = new ViewHolder();  
                    convertView = LayoutInflater.from(mContext).inflate(  
                              R.layout.listitem_notelist, null);  
                    holder.textview = (TextView) convertView  
                              .findViewById(R.id.tv_notelist);  
                    holder.checkbox = (CheckBox) convertView  
                              .findViewById(R.id.checkBox_notelist); 
                    holder.cancel = (ImageView) convertView  
                            .findViewById(R.id.imageView_cancel); 
                    
                    convertView.setTag(holder);  
                    
               } else {  
                    holder = (ViewHolder) convertView.getTag();  
               }  
               holder.cancel.setVisibility(View.GONE);
               holder.checkbox.setId(position);  
               holder.textview.setId(position);  
               holder.checkbox.setOnClickListener(new OnClickListener() {  
                    public void onClick(View v) {  
                         // TODO Auto-generated method stub  
                         CheckBox cb = (CheckBox) v;  
                         int id = cb.getId();  
                         if (thumbnailsselection[id]) {  
                              cb.setChecked(false);  
                              thumbnailsselection[id] = false; 
                              holder.textview.setTextColor(Color.parseColor("#000000"));
                             
                              Log.v("Check","check");
                         } else {  
                              cb.setChecked(true);  
                              thumbnailsselection[id] = true;  
                              Log.v("Check1","check");
                              holder.textview.setTextColor(Color.parseColor("#CCCCCC"));
                              holder.textview.setPaintFlags(holder.textview.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
                         }  
                         
                         
                       
                    }  
               });  
               holder.textview.setOnClickListener(new OnClickListener() {  
                    public void onClick(View v) {  
                         v.getId();  
                    }  
               });  
               holder.textview.setText(Constants.notelist.get(position));  
               holder.checkbox.setChecked(thumbnailsselection[position]);  
               holder.id = position;  
               return convertView;  
          }  
     }  
     class ViewHolder {  
          TextView textview;  
          CheckBox checkbox;  
          ImageView cancel;
          int id;  
     }  
     
     @Override
    public void onBackPressed() {
    	// TODO Auto-generated method stub
    	 Intent i = new Intent(NoteList.this,Notes.class);
    	 startActivity(i);
    	 Constants.ashish = "Checklist";
    	 finish();
    	// Log.v("Back", Constants.notelist.toString());
    	super.onBackPressed();
    }
     
     
}